package com.haoran.dwz.domain.entities;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 角色
 * 基于角色的权限控制
 */
@Table(name = "t_role")
@Entity
@ApiModel("系统用户角色")
public class Role extends AbstractEntity implements GrantedAuthority {

	private static final long serialVersionUID = 1426130233601528955L;

	/** 角色名 */
	@Column(unique = true)
	private String name;

	/**
	 * 职权编码
	 */
	@Column(unique = true)
	private String authority;
	
	/**
	 * 当用户为多个角色的时候 默认给一个优选级比较高的角色
	 */
	private Integer  priority;
	

	/** 描述 */
	private String description;

//	@ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
//	private List<User> users = new ArrayList<User>();
	
	@ApiModelProperty(hidden=true)
	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "t_role_menu", joinColumns = {
			@JoinColumn(name = "role_id", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "menu_id", referencedColumnName = "id") })
	private Set<Menu> menus = new HashSet<Menu>();
	

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "t_role_permission", joinColumns = {
			@JoinColumn(name = "role_id", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "permission_id", referencedColumnName = "id") })
	private Set<Permission> permissions = new HashSet<Permission>();
	
	
	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "t_role_group", joinColumns = {
			@JoinColumn(name = "role_id", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "group_id", referencedColumnName = "id") })
	private Set<Group> groups = new HashSet<Group>();

	public Set<Group> getGroups() {
		return groups;
	}

	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}

	public void setPriority(Integer priority) {
		this.priority = priority;
	}

	public Role setName(String name) {
		this.name = name;
		return this;
	}

	public String getDescription() {
		return description;
	}

	public Role setDescription(String description) {
		this.description = description;
		return this;
	}

	public String getName() {
		return name;
	}

//	public List<User> getUsers() {
//		return users;
//	}
//
//	public void setUsers(List<User> users) {
//		this.users = users;
//	}
	public Set<Menu> getMenus() {
		return menus;
	}

	public void setMenus(Set<Menu> menus) {
		this.menus = menus;
	}
	
	public Set<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<Permission> permissions) {
		this.permissions = permissions;
	}

	@Override
	public String getAuthority() {
		return authority;
	}

	public void setAuthority(String authority) {
		this.authority = authority;
	}

	public Integer getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

}
